﻿Imports Automation.BDaq
Imports System.ComponentModel

Public Class AdDevice
    Public WithEvents card As WaveformAiCtrl
    Private _aiCardInitialized As Boolean = False '板卡是否已经启动
    Public buffAiData As Double() '所有通道的原始数据
    Public axisXData As Double() 'X坐标数据
    Public selectedChs As New List(Of Integer) '选中的通道，通道号必须连续
    Public axisXMax As Double '视窗长度
    ''' <summary>
    ''' 设备类
    ''' </summary>
    ''' <param name="c">板卡的引用</param>
    ''' <remarks></remarks>
    Public Sub New(ByRef c As WaveformAiCtrl)
        card = c
    End Sub

    ''' <summary>
    ''' 启动采样
    ''' </summary>
    ''' <param name="rate">采样率</param>
    ''' <param name="sectionLen">每个通道的采样数</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function begin(rate As Integer, sectionLen As Integer) As Boolean
        If card.State <> ControlState.Running Then
            card.Conversion.ChannelCount = selectedChs.Count
            card.Conversion.ChannelStart = selectedChs(0)
            card.Conversion.ClockRate = rate
            card.Record.SectionCount = 0 '无限采样，直到手动结束
            card.Record.Cycles = 1
            card.Record.SectionLength = sectionLen
            For i As Integer = 0 To 7
                card.Channels(i).ValueRange = ValueRange.V_Neg5To5
            Next
            If BufferAI.InitAi(card) = ErrorCode.Success Then
                _aiCardInitialized = True
                Return True
            Else
                Return False
            End If
        Else
            Return False
        End If
    End Function

    Public Sub [stop]()
        card.Stop()
    End Sub

End Class

#Region "单个通道类（用于画图显示）"
Public Class AdChannel
    Public Shared lineColors As Color() = {Color.Red, Color.Black, Color.Blue, Color.Green, Color.Yellow, Color.Purple, Color.Pink, Color.SaddleBrown} '默认的曲线颜色

    Public buffer() As Double '数据缓存

    <DescriptionAttribute("曲线备注")> _
    Public Property brief As String

    <DescriptionAttribute("曲线的颜色")> _
    Public Property lineColor As Color

    <DescriptionAttribute("曲线粗细")> _
    Public Property lineWidth As Integer

    <DescriptionAttribute("最大值")> _
    Public Property max As Double

    <DescriptionAttribute("最小值")> _
    Public Property min As Double

    <DescriptionAttribute("数据变比")> _
    Public Property ratio As Double

    <DescriptionAttribute("通道编号")> _
    Public Property index As Integer

    '真实值为 (vi + offset) * ratio
    <DescriptionAttribute("直流偏移量")> _
    Public Property offset As Double

    Public Sub New(setting As AiSetting)
        lineColor = setting.lineColor
        index = setting.index
        max = setting.max
        min = setting.min
        lineWidth = setting.lineWidth
        ratio = setting.ratio
        offset = setting.offset
        brief = setting.brief
    End Sub

    ''' <summary>
    ''' 从Ad设备中提取每个通道的数据
    ''' </summary>
    ''' <param name="ad"></param>
    ''' <remarks></remarks>
    Public Sub GetData(ad As AdDevice)
        BufferAI.GetChannelData(ad.card, ad.buffAiData, ad.selectedChs.IndexOf(Me.index), Me.buffer, ratio:=Me.ratio, offset:=Me.offset)
    End Sub
End Class
#End Region
